﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nvn.Core.MVP;
using System.Data;
using System.Configuration;
using Hutech.CRM.Library.Models.DataAccessLayer;
using Hutech.CRM.Library.Models.Entities;

namespace Hutech.CRM.Library.Models.BusinessAccessLayer
{
    public class DanhMucThongTinDAL
    {

        /// <summary>
        /// Inserts a row in the dm_DanhMucThongTin table. Returns rows affected count.
        /// </summary>
        /// <param name="danhMucGuid"> danhMucGuid </param>
        /// <param name="danhMucID"> danhMucID </param>
        /// <param name="danhMucName"> danhMucName </param>
        /// <param name="loaiDanhMucGuid"> loaiDanhMucGuid </param>
        /// <param name="loaiDanhMucName"> loaiDanhMucName </param>
        /// <param name="isDeleted"> isDeleted </param>
        /// <param name="isActive"> isActive </param>
        /// <returns>int</returns>
        public static int Create(DanhMucThongTin item)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_Insert", 7);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.DanhMucGuid);
            sph.DefineSqlParameter("@DanhMucID", SqlDbType.NVarChar, 25, ParameterDirection.Input, item.DanhMucID);
            sph.DefineSqlParameter("@DanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.DanhMucName);
            sph.DefineSqlParameter("@LoaiDanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.LoaiDanhMucGuid);
            sph.DefineSqlParameter("@LoaiDanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.LoaiDanhMucName);
            sph.DefineSqlParameter("@IsDeleted", SqlDbType.Bit, ParameterDirection.Input, item.IsDeleted);
            sph.DefineSqlParameter("@IsActive", SqlDbType.Bit, ParameterDirection.Input, item.IsActive);
            int rowsAffected = sph.ExecuteNonQuery();
            return rowsAffected;

        }


        /// <summary>
        /// Updates a row in the dm_DanhMucThongTin table. Returns true if row updated.
        /// </summary>
        /// <param name="danhMucGuid"> danhMucGuid </param>
        /// <param name="danhMucID"> danhMucID </param>
        /// <param name="danhMucName"> danhMucName </param>
        /// <param name="loaiDanhMucGuid"> loaiDanhMucGuid </param>
        /// <param name="loaiDanhMucName"> loaiDanhMucName </param>
        /// <param name="isDeleted"> isDeleted </param>
        /// <param name="isActive"> isActive </param>
        /// <returns>bool</returns>
        public static bool Update(
            DanhMucThongTin item)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_Update", 7);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.DanhMucGuid);
            sph.DefineSqlParameter("@DanhMucID", SqlDbType.NVarChar, 25, ParameterDirection.Input, item.DanhMucID);
            sph.DefineSqlParameter("@DanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.DanhMucName);
            sph.DefineSqlParameter("@LoaiDanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, item.LoaiDanhMucGuid);
            sph.DefineSqlParameter("@LoaiDanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, item.LoaiDanhMucName);
            sph.DefineSqlParameter("@IsDeleted", SqlDbType.Bit, ParameterDirection.Input, item.IsDeleted);
            sph.DefineSqlParameter("@IsActive", SqlDbType.Bit, ParameterDirection.Input, item.IsActive);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Deletes a row from the dm_DanhMucThongTin table. Returns true if row deleted.
        /// </summary>
        /// <param name="danhMucGuid"> danhMucGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            Guid danhMucGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_Delete", 1);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, danhMucGuid);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

        /// <summary>
        /// Gets an IDataReader with one row from the dm_DanhMucThongTin table.
        /// </summary>
        /// <param name="danhMucGuid"> danhMucGuid </param>
        public static IDataReader GetOne(
            Guid danhMucGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_SelectOne", 1);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, danhMucGuid);
            return sph.ExecuteReader();

        }

        /// <summary>
        /// Gets a count of rows in the dm_DanhMucThongTin table.
        /// </summary>
        public static int GetCount()
        {

            return Convert.ToInt32(SqlHelper.ExecuteScalar(
                ConnectionStringStatic.GetWriteConnectionString(),
                CommandType.StoredProcedure,
                "dm_DanhMucThongTin_GetCount",
                null));

        }

        /// <summary>
        /// Gets an IDataReader with all rows in the dm_DanhMucThongTin table.
        /// </summary>
        public static IDataReader GetAll()
        {

            return SqlHelper.ExecuteReader(
                ConnectionStringStatic.GetWriteConnectionString(),
                CommandType.StoredProcedure,
                "dm_DanhMucThongTin_SelectAll",
                null);

        }

        /// <summary>
        /// Gets a page of data from the dm_DanhMucThongTin table.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static IDataReader GetPage(
            int pageNumber,
            int pageSize,
            out int totalPages)
        {
            totalPages = 1;
            int totalRows
                = GetCount();

            if (pageSize > 0) totalPages = totalRows / pageSize;

            if (totalRows <= pageSize)
            {
                totalPages = 1;
            }
            else
            {
                int remainder;
                Math.DivRem(totalRows, pageSize, out remainder);
                if (remainder > 0)
                {
                    totalPages += 1;
                }
            }

            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_SelectPage", 2);
            sph.DefineSqlParameter("@PageNumber", SqlDbType.Int, ParameterDirection.Input, pageNumber);
            sph.DefineSqlParameter("@PageSize", SqlDbType.Int, ParameterDirection.Input, pageSize);
            return sph.ExecuteReader();

        }


        internal static IDataReader GetAllKind()
        {
            return SqlHelper.ExecuteReader(
                ConnectionStringStatic.GetWriteConnectionString(),
                CommandType.StoredProcedure,
                "dm_DanhMucThongTin_GetAllKind",
                null);
        }

        internal static IDataReader CheckExitLoaDanhMuc(string loaiDanhMucName, Guid loaiDanhMucGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_CheckExitLoaDanhMuc", 2);
            sph.DefineSqlParameter("@DanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, loaiDanhMucName);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, loaiDanhMucGuid);
            return sph.ExecuteReader();
        }

        internal static IDataReader GetAllKindByIsActive(bool isActive, Guid loaiDanhMucGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_GetAllKindByIsActive", 2);
            sph.DefineSqlParameter("@IsActive", SqlDbType.Bit, ParameterDirection.Input, isActive);
            sph.DefineSqlParameter("@LoaiDanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, loaiDanhMucGuid);
            return sph.ExecuteReader();
        }

        internal static bool DeleteKind(Guid DataKeyValue)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_DeleteKind", 1);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, DataKeyValue);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);
        }

        internal static IDataReader CheckExitDanhMuc(string danhMucName,Guid loaiDanhMucGuid)
        {
            SqlParameterHelper sph = new SqlParameterHelper(ConnectionStringStatic.GetWriteConnectionString(), "dm_DanhMucThongTin_CheckExitDanhMuc", 2);
            sph.DefineSqlParameter("@DanhMucName", SqlDbType.NVarChar, 256, ParameterDirection.Input, danhMucName);
            sph.DefineSqlParameter("@DanhMucGuid", SqlDbType.UniqueIdentifier, ParameterDirection.Input, loaiDanhMucGuid);
            return sph.ExecuteReader();
        }
    }

}

